<template>
  <view class="tab-bar" :style="{bottom:safeAreaBottom ? '0rpx' : '-34rpx'}">
    <view class="tab-list">
      <view v-for="(item, index) in tabList" :key="item.id" :class="item.bigType ? 'tab-item-big' : 'tab-item'"
        @click="selectTabbar(item, index)">
        <template v-if="item.icon && item.activeIcon">
          <image v-if="index === showIndex" :src="item.activeIcon"></image>
          <image v-else :src="item.icon"></image>
        </template>
        <template v-else>
          <view class="empty-icon"></view>
        </template>
        <view class="tab-name" :style="{ color: index === showIndex ? activeColor : color }">{{ item.name }}</view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    tabList: {
      type: Array,
      default() {
        return []
      }
    },
    index: {
      type: Number,
      default: 0
    },
    color: {
      type: String,
      default: '#999'
    },
    activeColor: {
      type: String,
      default: '#3A76EE'
    },
    isNeedSetNavTitle: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      safeAreaBottom: '',
      showIndex: 0
    }
  },
  watch: {
    index: {
      deep: true,
      immediate: true,
      handler(inx) {
        this.showIndex = inx
      }
    }
  },
  mounted() {
    if (this.isNeedSetNavTitle) {
      uni.setNavigationBarTitle({
        title: this.tabList[this.showIndex]?.name
      })
    }
    uni.getSystemInfo({
      success: res => {
				console.log(res.safeAreaInsets.bottom)
				this.safeAreaBottom = res.safeAreaInsets.bottom
      }
    })
  },
  methods: {
    selectTabbar(item, index) {
      if (this.showIndex !== index) {
        this.showIndex = index
        this.$emit('change', item)
      }
      if (this.isNeedSetNavTitle) {
        uni.setNavigationBarTitle({
          title: this.tabList[this.showIndex]?.name
        })
      }
    }
  }
}
</script>

<style lang='scss' scoped>
.tab-bar {
  width: 100%;
  height: 194rpx;
  position: fixed;
  bottom: 0;
  left: 0px;
  right: 0px;
  z-index: 99999;
  display: flex;
	align-items: center;
	justify-content: center;
	background-image: url('@/static/tabBar/page-bg.png');
  background-size: 100% 100%;
  border-radius: 12rpx 12rpx 0 0;
  background-repeat: no-repeat;
}

.tab-list {
  width: calc(100% - 40rpx);
  padding: 0 20rpx;
  display: flex;
  justify-content: space-around;
  margin-top: 0rpx;
}

.tab-item {
  width: 20%;
  display: flex;
  align-items: center;
  flex-direction: column;
  font-size: 24rpx;
  color: #3982A5;
}

.tab-item-big {
  width: 20%;
  display: flex;
  align-items: center;
  flex-direction: column;
  font-size: 24rpx;
  color: #3982A5;
  margin-top: -20rpx;
  margin-left: 2rpx;
  position: relative;
}

.tab-item image {
  width: 48rpx;
  height: 48rpx;
  margin-bottom: 8rpx;
}

.tab-item-big image {
  width: 64rpx;
  height: 64rpx;
  border-radius: 50%;
  margin:0 auto 10rpx auto;
}

.empty-icon {
  width: 48rpx;
  height: 48rpx;
  margin-bottom: 8rpx;
  border-radius: 50%;
}

.tab-name {
  text-align: center;
  line-height: 24rpx;
  margin-top: 4rpx;
}
</style>
